<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.noticeName"
        placeholder="请输入文章标题"
        class="filter-item search-item"
      />
      <el-input
        v-model="listQuery.author"
        placeholder="请输入文章作者"
        class="filter-item search-item"
      />
      <el-button class="filter-item" type="primary" plain @click="search">
        {{ $t("table.search") }}
      </el-button>
      <el-button class="filter-item" type="warning" plain @click="reset">
        {{ $t("table.reset") }}
      </el-button>
      <el-button class="filter-item" type="primary" plain @click="addNotice">
        新增
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column align="center" label="ID">
        <template slot-scope="scope">
          <span>{{ scope.row.noticeId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="Date">
        <template slot-scope="scope">
          <span>{{
            scope.row.createDate | parseTime("{y}-{m}-{d} {h}:{i}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="Author">
        <template slot-scope="scope">
          <span>{{ scope.row.author }}</span>
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="Status">
        <template slot-scope="{ row }">
          <el-tag :type="row.noticeState | statusFilter">
            {{ row.noticeState }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="Importance">
        <template slot-scope="{ row }">
          <el-rate v-model="row.importance" :colors="colors" disabled />
        </template>
      </el-table-column>
      <el-table-column min-width="200px" label="Title">
        <template slot-scope="{ row }">
          <router-link
            :to="{
              path: '/defend/notice/edit',
              query: { noticeId: row.noticeId, action: true },
            }"
            class="link-type"
          >
            <span>{{ row.noticeName }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Actions">
        <template slot-scope="scope">
          <router-link
            :to="{
              path: '/defend/notice/edit',
              query: { noticeId: scope.row.noticeId, action: true },
            }"
          >
            <el-button type="primary" size="small" icon="el-icon-edit">
              Edit
            </el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'; // Secondary package based on el-pagination

export default {
  name: 'ArticleList',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        2: 'info',
        3: 'danger'
      };
      return statusMap[status];
    }
  },
  data() {
    return {
      colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 20
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      this.$get('study/lNotice/list', {
        ...this.listQuery
      }).then(response => {
        this.list = response.data.resultBody.rows;
        this.total = response.data.resultBody.total;
        this.listLoading = false;
      });
    },

    addNotice() {
      this.$router.push({
        path: '/defend/notice/edit',
        query: { action: false }
      });
    },

    search() {
      this.listQuery.pageNum = 1;
      this.listLoading = true;
      this.$get('study/lNotice/list', {
        ...this.listQuery
      }).then(response => {
        this.list = response.data.resultBody.rows;
        this.total = response.data.resultBody.total;
        this.listLoading = false;
      });
    },

    reset() {
      this.listQuery = {
        pageNum: 1,
        pageSize: 20,
        author: '',
        noticeName: ''
      };
      this.listLoading = true;
      this.$get('study/lNotice/list', {
        ...this.listQuery
      }).then(response => {
        this.list = response.data.resultBody.rows;
        this.total = response.data.resultBody.total;
        this.listLoading = false;
      });
    }
  }
};
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
